﻿using EyFramework.Common;
using EyFramework.ORM;
using SqlSugar;
using System;

namespace EyFramework.Sys.Core.Service.Impl
{
    /// <summary>
    /// 使用数据库保存异常日志信息
    /// </summary>
    public class DBExceptionHandler : IExceptionHandler
    {
        private BaseDaoHis<object> baseDao = new BaseDaoHis<object>();

        /// <summary>
        /// 保存日志，使用存储过程独立事务
        /// </summary>
        /// <param name="ex"></param>
        public void SaveException(Exception ex)
        {
            var pModulename = new SugarParameter("@P_MODULENAME",
                Globals.CurrentUser == null ? "" : Globals.CurrentUser.SysName);
            var pExceptionMessage = new SugarParameter("@P_EXCEPTION_MESSAGE", ex.Message);
            var pExceptionContent = new SugarParameter("@P_EXCEPTION_CONTENT", ex.ToString());
            var pIp = new SugarParameter("@P_IP", GetSystemInfo.GetClientLocalIPv4Address());
            var pStaffCode = new SugarParameter("@P_STAFF_CODE", Globals.CurrentUser == null ? "" : Globals.CurrentUser.StaffCode);
            var pStaffName = new SugarParameter("@P_STAFF_NAME", Globals.CurrentUser == null ? "" : Globals.CurrentUser.StaffName);
            var pLogId = new SugarParameter("@P_LOG_ID", null, true);
            try
            {
                baseDao.Db.Ado.UseStoredProcedure().GetDataTable("SAVE_EXCEPTION_LOG",
                    pModulename,
                    pExceptionMessage,
                    pExceptionContent,
                    pIp,
                    pStaffCode,
                    pLogId);
            }
            catch (Exception ee)
            {
                Logger.Error("异常日志记录失败," + ee);
            }
        }
    }
}